package br.elsa.survey.domain.questions;

import java.io.Serializable;

import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Table;

import org.hibernate.validator.constraints.NotEmpty;

/**
 * JPA {@link Entity} that abstracts an option of a multiple choice question
 * 
 * @author diego
 * @since 2010-10-07
 */
@Entity
@Table(name = "options", schema = "survey")
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "TYPE", discriminatorType = DiscriminatorType.STRING)
public abstract class Option implements Serializable {

	private static final long serialVersionUID = -5192243074240746843L;

	/**
	 * Option unique identifier
	 * <p>
	 * It will resolve to a primary key in a OR mapping.
	 * </p>
	 */
	@Id
	@GeneratedValue(strategy = GenerationType.SEQUENCE)
	protected Integer id;

	/**
	 * Option text
	 */
	@NotEmpty(message = "The option text can not be empty")
	protected String text;

	public Integer getId() {
		return this.id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getText() {
		return this.text;
	}

	public void setText(String text) {
		this.text = text;
	}

	@Override
	public boolean equals(Object obj) {
		if (!(obj instanceof Option)) {
			return false;
		}

		if (this == obj) {
			return true;
		}

		Option choice = (Option) obj;
		if (this == choice) {
			return true;
		}

		if (this.getId() == null && choice.getId() == null || this.getId().equals(choice.getId())) {

			return true;
		}

		return false;
	}
}
